using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer;
using System;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.SqlServer.Management.SqlManagerUI
{
using Server = Microsoft.SqlServer.Management.Smo.Server;
	internal class CUtils
	{
		private const int ObjectPermissionsDeniedErrorNumber = 229;

		private const int ColumnPermissionsDeniedErrorNumber = 230;

		public static Bitmap LoadBitmap(string szBitmapName)
		{
			string name = typeof(CUtils).Namespace + ".Images." + szBitmapName;
			Stream manifestResourceStream = typeof(CUtils).Assembly.GetManifestResourceStream(name);
			if (manifestResourceStream != null)
			{
				return new Bitmap(manifestResourceStream);
			}
			return null;
		}

		public static Icon LoadIcon(string strName)
		{
			string name = typeof(CUtils).Namespace + ".Images." + strName;
			Stream manifestResourceStream = typeof(CUtils).Assembly.GetManifestResourceStream(name);
			if (manifestResourceStream != null)
			{
				return new Icon(manifestResourceStream, 16, 16);
			}
			return null;
		}

		public static void LoadAddIcon(ImageList imageList, string strName)
		{
			Icon icon = null;
			string name = typeof(CUtils).Namespace + ".Images." + strName;
			Stream manifestResourceStream = typeof(CUtils).Module.Assembly.GetManifestResourceStream(name);
			if (manifestResourceStream != null)
			{
				try
				{
					icon = new Icon(manifestResourceStream, 16, 16);
					imageList.Images.Add(icon);
				}
				finally
				{
					if (icon != null)
					{
						icon.Dispose();
					}
				}
			}
		}

		public static void UseMaster(Server server)
		{
			server.ConnectionContext.ExecuteNonQuery("use master");
		}

		public static int GetBorderHeight(BorderStyle style)
		{
			if (style == BorderStyle.FixedSingle)
			{
				return SystemInformation.BorderSize.Height;
			}
			if (style == BorderStyle.Fixed3D)
			{
				return SystemInformation.Border3DSize.Height;
			}
			return 0;
		}

		public static int GetBorderWidth(BorderStyle style)
		{
			if (style == BorderStyle.FixedSingle)
			{
				return SystemInformation.BorderSize.Width;
			}
			if (style == BorderStyle.Fixed3D)
			{
				return SystemInformation.Border3DSize.Width;
			}
			return 0;
		}

		public static Server GetSmoServer(IManagedConnection mc)
		{
			if (mc.Connection == null)
			{
				throw new ArgumentNullException("ci");
			}
			Server server = null;
			if (server == null)
			{
				throw new InvalidOperationException();
			}
			return server;
		}

		public static int GetServerVersion(Server server)
		{
			return server.Information.Version.Major;
		}

		public static DateTime GetOldestDate(int numUnits, TimeUnitType typeUnits)
		{
			DateTime result = DateTime.Now;
			switch (typeUnits)
			{
			case TimeUnitType.Week:
				result = DateTime.Now.AddDays((double)(-7 * numUnits));
				break;
			case TimeUnitType.Month:
				result = DateTime.Now.AddMonths(-1 * numUnits);
				break;
			case TimeUnitType.Year:
				result = DateTime.Now.AddYears(-1 * numUnits);
				break;
			default:
				result = DateTime.Now.AddDays((double)(-1 * numUnits));
				break;
			}
			return result;
		}

		public static string TokenizeXml(string s)
		{
			if (s == null)
			{
				return string.Empty;
			}
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < s.Length; i++)
			{
				char c = s[i];
				char c2 = c;
				if (c2 != '&')
				{
					switch (c2)
					{
					case '<':
						stringBuilder.Append("&lt;");
						goto IL_70;
					case '>':
						stringBuilder.Append("&gt;");
						goto IL_70;
					}
					stringBuilder.Append(c);
				}
				else
				{
					stringBuilder.Append("&amp;");
				}
				IL_70:;
			}
			return stringBuilder.ToString();
		}

		public static SqlException GetSqlException(Exception e)
		{
			SqlException ex = null;
			for (Exception ex2 = e; ex2 != null; ex2 = ex2.InnerException)
			{
				ex = (ex2 as SqlException);
				if (ex != null)
				{
					break;
				}
			}
			return ex;
		}

		public static string GetMachineName(string sqlServerName)
		{
			if (sqlServerName.Trim().Length != 0)
			{
				return sqlServerName.Split(new char[]
				{
					'\\'
				})[0];
			}
			return sqlServerName;
		}

		public static bool IsPermissionDeniedException(SqlException sqlException)
		{
			bool result = false;
			if (sqlException.Errors != null)
			{
				foreach (SqlError sqlerror in sqlException.Errors)
				{
					int sqlErrorNumber = CUtils.GetSqlErrorNumber(sqlerror);
					if (229 == sqlErrorNumber || 230 == sqlErrorNumber)
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public static int GetSqlErrorNumber(SqlError sqlerror)
		{
			return sqlerror.Number;
		}

		public static string EscapeString(string s, char cEsc)
		{
			StringBuilder stringBuilder = new StringBuilder(s.Length * 2);
			for (int i = 0; i < s.Length; i++)
			{
				char c = s[i];
				stringBuilder.Append(c);
				if (cEsc == c)
				{
					stringBuilder.Append(c);
				}
			}
			return stringBuilder.ToString();
		}

		public static string EscapeStringCBracket(string s)
		{
			return CUtils.EscapeString(s, ']');
		}

		public static string EscapeStringSQuote(string s)
		{
			return CUtils.EscapeString(s, '\'');
		}

		public static string UnEscapeString(string s, char cEsc)
		{
			StringBuilder stringBuilder = new StringBuilder(s.Length);
			bool flag = false;
			for (int i = 0; i < s.Length; i++)
			{
				char c = s[i];
				if (cEsc == c)
				{
					if (flag)
					{
						flag = false;
					}
					else
					{
						stringBuilder.Append(c);
						flag = true;
					}
				}
				else
				{
					stringBuilder.Append(c);
					flag = false;
				}
			}
			return stringBuilder.ToString();
		}

		public static string UnEscapeStringCBracket(string s)
		{
			return CUtils.UnEscapeString(s, ']');
		}

		public static string UnEscapeStringSQuote(string s)
		{
			return CUtils.UnEscapeString(s, '\'');
		}
	}
}
